package com.dreamer.service.mobile.impl;

import com.dreamer.domain.account.GoodsAccount;
import com.dreamer.domain.mall.goods.Goods;
import com.dreamer.domain.mall.goods.Price;
import com.dreamer.domain.user.Agent;
import com.dreamer.domain.user.AgentLevel;
import com.dreamer.domain.user.MutedUser;
import com.dreamer.repository.mobile.GoodsDao;
import com.dreamer.service.mobile.*;
import com.dreamer.util.PreciseComputeUtil;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Order;
import org.omg.CORBA.portable.ApplicationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ps.mx.otter.utils.SearchParameter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by huangfei on 03/07/2017.
 */
@Service
public class GoodsHandlerImpl extends BaseHandlerImpl<Goods> implements GoodsHandler {

    @Override
    public List<Goods> findGoods(SearchParameter<Goods> parameter) {
        Example example = Example.create(parameter.getEntity());
        DetachedCriteria criteria = DetachedCriteria.forClass(Goods.class);
        criteria.add(example);
        criteria.addOrder(Order.asc("order"));
        return goodsDao.searchByPage(parameter, criteria);
    }


    @Override
    @Transactional
    public void addBalance(Integer gid, Integer quantity) {
        Goods goods = goodsDao.get(gid);
        goods.increaseCurrentBalance(quantity);
        goodsDao.merge(goods);
    }

    @Override
    @Transactional
    public void reduceBalacne(Integer gid, Integer quantity) {
        Goods goods = goodsDao.get(gid);
        goods.deductCurrentBalance(quantity);
        goodsDao.merge(goods);
    }

    @Override
    @Transactional
    public void addStock(Integer gid, Integer quantity) {
        Goods goods = goodsDao.get(gid);
        goods.increaseCurrentStock(quantity);
        goodsDao.merge(goods);
    }

    @Override
    @Transactional
    public void reduceStock(Integer gid, Integer quantity) {
        Goods goods = goodsDao.get(gid);
        goods.deductCurrentStock(quantity);
        goodsDao.merge(goods);
    }

    @Override
    @Transactional
    public void addSumStock(Integer gid, Integer quantity) {
        Goods goods = goodsDao.get(gid);
        goods.addSumStock(quantity);
        goodsDao.merge(goods);
    }

    @Override
    @Transactional
    public void reduceSumStock(Integer gid, Integer quantity) {
        Goods goods = goodsDao.get(gid);
        goods.reduceSumStock(quantity);
        goodsDao.merge(goods);
    }


    @Override
    @Transactional(rollbackFor = ApplicationException.class)
    public void adminAddSumStock(Integer gid, Integer quantity) {
        MutedUser mutedUser = muteUserHandler.getMuteUser();
        Goods goods = goodsHandler.get(gid);
        GoodsAccount goodsAccount = goodsAccountHandler.getGoodsAccount(mutedUser, goods, goodsAccountHandler.getMainGoodsAccount(mutedUser));
        if (quantity > 0) {
            //增加总入库
            goodsHandler.addSumStock(gid, quantity);
            //增加库房库存
            goodsHandler.addStock(gid, quantity);
            //增加公司余额
            goodsAccountHandler.increaseGoodsAccount(goodsAccount, quantity);
            goodsAccountHandler.merge(goodsAccount);
        } else {//减少操作
            //减少总库存
            goodsHandler.reduceSumStock(gid, -quantity);
            //减少库房库存
            goodsHandler.reduceStock(gid, -quantity);
            //减少公司余额  公司账户余额为空
//            goodsAccountHandler.deductGoodsAccount(goodsAccount,quantity);
            goodsAccountHandler.merge(goodsAccount);
        }
    }

    @Override
    public Map<Integer, Integer> sumAdminBalance() {
        return null;
    }

    @Autowired
    private AgentHandler agentHandler;

    @Autowired
    private AgentLevelHandler agentLevelHandler;

    @Autowired
    private PriceHandler priceHandler;

    @Override
    public List calProfit(Integer userId, Integer goodsId) {
        List res = new ArrayList();

        AgentLevel agentLevel = agentHandler.getLevel(agentHandler.get(userId));
        Goods goods = goodsHandler.get(goodsId);
        Price myPrice = priceHandler.getPrice(agentLevel, goods);
        Integer lv = agentLevel.getLevel() + 1;
        //如果比他低的级别还存在
        AgentLevel lt;
        while ((lt = agentLevelHandler.get("level", lv)) != null) {
            Map<String, Double> tem = new HashMap<>();
            Price price = priceHandler.getPrice(lt, goods);
            //存储级别差价 就是利润
            tem.put(lt.getName(), PreciseComputeUtil.round(price.getPrice() - myPrice.getPrice()));
            res.add(tem);
            lv++;
        }
        return res;
    }

    private GoodsDao goodsDao;

    public GoodsDao getGoodsDao() {
        return goodsDao;
    }

    @Autowired
    private GoodsHandler goodsHandler;

    @Autowired
    private GoodsAccountHandler goodsAccountHandler;

    @Autowired
    private MuteUserHandler muteUserHandler;

    @Autowired
    public void setGoodsDao(GoodsDao goodsDao) {
        this.goodsDao = goodsDao;
        super.setBaseDao(goodsDao);
    }
}
